<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.phperskill.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace App\Admin;

use Model\Admin\MenuModel as MenuModel;
use Core\Cache\FileCache as FileCache;

/**
 * Pes 登陆模块
 * @author LazyPHP
 * @version 1.0
 */
class Login extends Common {

    public function __construct() {
        if (empty($_SESSION['pes_login'])) {
            header('HTTP/1.1 404 Page no found');
            exit;
        }
        if (!empty($_SESSION['admin'])) {
            header('Location: /?g=Admin&m=Index&a=index');
        }
    }
    
    public function index(){
        $this->display();
    }

    /**
     * 执行登陆处理
     */
    public function dologin() {
        //$this->chedkToken();
        $account = $this->is_p('account', '请输入账号');
        $password = md5(md5($this->is_p('password', '请输入密码')));

        $db = $this->db('user');
        $data['account'] = $account;
        $data['password'] = $password;
        $result = $db->where('account = :account and password = :password')->find($data);
        $this->checkNode($result['user_group_id']);
        if ($result == FALSE) {
            $this->error('账号或者密码不正确');
        } else {
            $_SESSION['admin']['user'] = $result;
            $fileCache = new FileCache();
            $fileCache->createCache('menu', MenuModel::getMenu());
            $this->success('登陆成功!', '/Admin-Index-index');
        }
    }

}
